-module(ex4).
-export([test/0]).

-record(circle, {radius}).
-record(rectangle, {length, width}).
-record(triangle, {a, b, c}).

perimeter(#circle{radius=Radius}) ->
    2 * math:pi() * Radius;
perimeter(#rectangle{length=Length, width=Width}) ->
    2 * (Length + Width);
perimeter(#triangle{a=A, b=B, c=C}) ->
    A + B + C.

area(#circle{radius=Radius}) ->
    math:pi() * Radius * Radius;
area(#rectangle{length=Length, width=Width}) ->
    Length * Width;
area(#triangle{a=A, b=B, c=C} = T) ->
    P = perimeter(T) / 2,
    math:sqrt(P * (P-A) * (P-B) *(P-C)).


test() ->
    Circle = #circle{radius=5},
    io:format("Circle ~p ~p~n", [perimeter(Circle), area(Circle)]),
    Rectangle = #rectangle{length=5, width=4},
    io:format("Rectangle ~p ~p~n", [perimeter(Rectangle), area(Rectangle)]),
    Triangle = #triangle{a=3,b=4,c=5},
    io:format("Triangle ~p ~p~n", [perimeter(Triangle), area(Triangle)]).
